package cn.kevinkun.KevinkunRegEx;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.SimpleFunction;
import com.google.appinventor.components.annotations.SimpleObject;
import com.google.appinventor.components.common.ComponentCategory;
import com.google.appinventor.components.runtime.AndroidNonvisibleComponent;
import com.google.appinventor.components.runtime.Component;
import com.google.appinventor.components.runtime.ComponentContainer;
import com.google.appinventor.components.runtime.ReplForm;
import com.google.appinventor.components.runtime.errors.YailRuntimeError;

import gnu.lists.LList;
import gnu.mapping.ProcedureN;
import gnu.mapping.SimpleSymbol;
import gnu.mapping.Symbol;
import kawa.standard.Scheme;

@DesignerComponent(version = 3, description = "规则表达式扩展，更多信息请参照https://www.kevinkun.cn/regex.html", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "images/extension.png")
@SimpleObject(external = true)
public class KevinkunRegEx extends AndroidNonvisibleComponent implements Component {
	public static final int VERSION = 3;

	public KevinkunRegEx(ComponentContainer container) {
		super(container.$form());

	}

	@SimpleFunction(description = "是否匹配规则表达式")
	public boolean IsMatch(String string, String pattern) {
		return string.matches(pattern);
	}

	@SimpleFunction(description = "是否电子邮箱")
	public boolean IsEmail(String string) {
		return string.matches("^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$");
	}

	@SimpleFunction(description = "是否数字")
	public boolean IsNumber(String string) {
		return string.matches("^(\\-|\\+)?\\d+(\\.\\d+)?$");
	}

	@SimpleFunction(description = "是否是强密码，至少有1个数字、1个小写字母、1个大写字母、至少8位")
	public boolean IsStrongPassword(String string) {
		return string.matches("^(?=.*\\d)(?=.*[a-z])(?=.*[A-Z]).{8,}$");
	}

	@SimpleFunction(description = "获取符合规则表达式的片段，返回列表")
	public List<String> GetMatches(String string, String pattern) {
		List<String> ls = new ArrayList<String>();
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(string);
		while (m.find()) {
			ls.add(m.group());
		}
		return ls;
	}

	@SimpleFunction(description = "")
	public List<List<Object>> GetMatchesWithPostion(String string, String pattern) {
		List<List<Object>> ls = new ArrayList<List<Object>>();
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(string);
		while (m.find()) {

			List<Object> temp = new ArrayList<Object>();
			temp.add(m.group());
			temp.add(m.start() + 1);
			temp.add(m.end());

			ls.add(temp);
		}
		return ls;
	}

	@SimpleFunction(description = "")
	public List<List<String>> GetMatchesWithGroup(String string, String pattern) {
		List<List<String>> ls = new ArrayList<List<String>>();
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(string);
		while (m.find()) {
			List<String> temp = new ArrayList<String>();
			for (int i = 0; i <= m.groupCount(); i++) {
				temp.add(m.group(i));
			}
			ls.add(temp);
		}
		return ls;
	}

	@SimpleFunction(description = "")
	public List GetMatchesWithGroupAndPosition(String string, String pattern) {
		List ls = new ArrayList<>();
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(string);
		while (m.find()) {
			List groups = new ArrayList<>();
			for (int i = 0; i <= m.groupCount(); i++) {
				List temp = new ArrayList<>();
				temp.add(m.group(i));
				temp.add(m.start(i) + 1);
				temp.add(m.end(i));
				groups.add(temp);
			}
			ls.add(groups);
		}
		return ls;
	}

	@SimpleFunction(description = "符合表达式的文本的位置")
	public int IndexOf(String string, String pattern) {
		Pattern p = Pattern.compile(pattern);
		Matcher m = p.matcher(string);
		if (m.find()) {
			return m.start() + 1;
		}
		return -1;
	}

	@SimpleFunction(description = "分割字串")
	public List SplitWith(String string, String pattern) {
		return Arrays.asList(string.split(pattern));
	}

	@SimpleFunction(description = "替换所有符合条件的字串")
	public String ReplaceAll(String originalStr, String pattern, String replaceStr) {
		return originalStr.replaceAll(pattern, replaceStr);
	}


	@SimpleFunction(description = "替换符合条件的第一个字串")
	public String ReplaceFirst(String originalStr, String pattern, String replaceStr) {
		return originalStr.replaceFirst(pattern, replaceStr);
	}

}
